package com.gec.dao.Impl;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public abstract class BaseDaoImpl<T> {
	private  static String driver="com.mysql.cj.jdbc.Driver";
	private  static String url="jdbc:mysql://localhost:3306/gll?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai";
	private  static String username="root";
	private  static String password="root";
	
	static{//静态构造方法,全局只运行一次
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	public T execOne(String sql,Object...args) {
		List<T> list= this.execQuery(sql, args);
		if(list.size()==0)
			return null;
		if(list.size()>1)
			throw new RuntimeException("查询不只一个对象");
		return (T)list.get(0);
	
	}
	public Object execSingle(String sql,Object...args) {
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try{
			conn=DriverManager.getConnection(url,username,password);
			pstmt=conn.prepareStatement(sql);
			if(args!=null&&args.length>0){
				int i=1;
				for(Object arg:args){
				   pstmt.setObject(i++, arg);
				}
			}
			Object o = null;
			rs = pstmt.executeQuery();
			while(rs.next()){
				o = rs.getObject(1);
			}
			return o;
		}catch(Exception ex){
			ex.printStackTrace();
			throw new RuntimeException(ex);
		}finally{
			this.closeAll(conn, pstmt, null);
		}
	}
	public List<T> execQuery(String sql,Object...args){
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try{
			conn=DriverManager.getConnection(url,username,password);
			pstmt=conn.prepareStatement(sql);
			if(args!=null&&args.length>0){
				int i=1;
				for(Object arg:args){
				   pstmt.setObject(i++, arg);
				}
			}
			List<T> list=new ArrayList<>();
			rs=pstmt.executeQuery();
			while(rs.next()){
				//这里的动作每个类都不同,父类不要实现,各子类自行填充
				T o=this.mapper(rs);
				list.add(o);
			}
			return list;
		}catch(Exception ex){
			ex.printStackTrace();
			throw new RuntimeException(ex);
		}finally{
			this.closeAll(conn, pstmt, null);
		}
	}
	
	protected abstract T mapper(ResultSet rs) throws SQLException;

	
	public int execUpdate(String sql,Object...args){
		Connection conn=null;
		PreparedStatement pstmt=null;
		try{
			conn=DriverManager.getConnection(url,username,password);
			pstmt=conn.prepareStatement(sql);
			if(args!=null&&args.length>0){
				int i=1;
				for(Object arg:args){
				   pstmt.setObject(i++, arg);
				}
			}
			int n=pstmt.executeUpdate();
			return n;
		}catch(Exception ex){
			ex.printStackTrace();
			throw new RuntimeException(ex);
		}finally{
			this.closeAll(conn, pstmt, null);
		}
	}
	
	public void closeAll(Connection conn,Statement stmt,ResultSet rs){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(stmt!=null){
			try {
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}


